﻿/*
* CRM_Customer_grow.cs
*
* 功 能： N/A
* 类 名： CRM_Customer_grow
*
* Ver    变更日期             负责人     变更内容
* ───────────────────────────────────
* V1.0  2016-03-03 19:39:19    黄润伟    
*
* Copyright © 2015 www.xhdcrm.com All rights reserved.
*┌──────────────────────────────────┐
*│　版权所有：小黄豆                      　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using XHD.DBUtility;//Please add references
namespace XHD.DAL
{
    /// <summary>
    /// 数据访问类:CRM_Customer_grow
    /// </summary>
    public partial class CRM_Customer_grow
    {
        public CRM_Customer_grow()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(XHD.Model.CRM_Customer_grow model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CRM_Customer_grow(");
            strSql.Append("id,xhs_code,xhs_notes,xhs_fun,xhs_hudong,xhs_guanzhu,xhs_gathertime,create_id,create_time)");
            strSql.Append(" values (");
            strSql.Append("@id,@xhs_code,@xhs_notes,@xhs_fun,@xhs_hudong,@xhs_guanzhu,@xhs_gathertime,@create_id,@create_time)");
            SqlParameter[] parameters = {
                new SqlParameter( "@id", SqlDbType.VarChar,500),
                new SqlParameter( "@xhs_code", SqlDbType.VarChar,500),
                new SqlParameter( "@xhs_notes", SqlDbType.Int,4),
                new SqlParameter( "@xhs_fun", SqlDbType.Int,4),
                new SqlParameter( "@xhs_hudong", SqlDbType.Int,4),
                new SqlParameter( "@xhs_guanzhu", SqlDbType.Int,4),
                new SqlParameter( "@xhs_gathertime", SqlDbType.DateTime),
                new SqlParameter( "@create_id", SqlDbType.VarChar,500),
                new SqlParameter( "@create_time", SqlDbType.DateTime)
            };
            parameters[0].Value = model.id;
            parameters[1].Value = model.xhs_code;
            parameters[2].Value = model.xhs_notes;
            parameters[3].Value = model.xhs_fun;
            parameters[4].Value = model.xhs_hudong;
            parameters[5].Value = model.xhs_guanzhu;
            parameters[6].Value = model.xhs_gathertime;
            parameters[7].Value = model.create_id;
            parameters[8].Value = model.create_time;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0 ? true : false;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(XHD.Model.CRM_Customer_grow model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CRM_Customer_grow set ");
            strSql.Append("xhs_code = @xhs_code,");
            strSql.Append("xhs_notes = @xhs_notes,");
            strSql.Append("xhs_fun = @xhs_fun,");
            strSql.Append("xhs_hudong = @xhs_hudong,");
            strSql.Append("xhs_guanzhu = @xhs_guanzhu,");
            strSql.Append("xhs_gathertime = @xhs_gathertime,");
            strSql.Append(" where id=@id ");

            SqlParameter[] parameters = {
                 new SqlParameter( "@xhs_code", SqlDbType.VarChar,500),
                new SqlParameter( "@xhs_notes", SqlDbType.Int,4),
                new SqlParameter( "@xhs_fun", SqlDbType.Int,4),
                new SqlParameter( "@xhs_hudong", SqlDbType.Int,4),
                new SqlParameter( "@xhs_guanzhu", SqlDbType.Int,4),
                new SqlParameter( "@xhs_gathertime", SqlDbType.DateTime),
                new SqlParameter( "@id", SqlDbType.VarChar,50)
            };
            parameters[0].Value = model.xhs_code;
            parameters[1].Value = model.xhs_notes;
            parameters[2].Value = model.xhs_fun;
            parameters[3].Value = model.xhs_hudong;
            parameters[4].Value = model.xhs_guanzhu;
            parameters[5].Value = model.xhs_gathertime;
            parameters[6].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0 ? true : false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CRM_Customer_grow ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,50)           };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0 ? true : false;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CRM_Customer_grow ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows > 0 ? true : false;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, bool isAll = false)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,xhs_code,xhs_notes,xhs_fun,xhs_hudong,xhs_guanzhu,xhs_gathertime,create_id,create_time ");
            strSql.Append("FROM CRM_Customer_grow ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder, bool isAll = false)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,C_name,C_sex,C_department,C_position,C_birthday,C_tel,C_fax,C_email,C_mob,C_QQ,C_add,C_hobby,C_remarks,customer_id,create_id,create_time ");
            strSql.Append(",(select cus_name from CRM_Customer where id = CRM_Customer_grow.customer_id ) as customer ");
            strSql.Append(" FROM CRM_Customer_grow ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string filedOrder, out string Total, bool isAll = false)
        {
            StringBuilder strSql_grid = new StringBuilder();
            StringBuilder strSql_total = new StringBuilder();
            strSql_total.Append(" SELECT COUNT(id) FROM CRM_Customer_grow ");
            strSql_grid.Append("SELECT ");
            strSql_grid.Append("      n,id,C_name,C_sex,C_department,C_position,C_birthday,C_tel,C_fax,C_email,C_mob,C_QQ,C_add,C_hobby,C_remarks,customer_id,create_id,create_time ");
            strSql_grid.Append(",(select cus_name from CRM_Customer where id = w1.customer_id ) as customer ");
            strSql_grid.Append(" FROM ( SELECT id,C_name,C_sex,C_department,C_position,C_birthday,C_tel,C_fax,C_email,C_mob,C_QQ,C_add,C_hobby,C_remarks,customer_id,create_id,create_time, ROW_NUMBER() OVER( Order by " + filedOrder + " ) AS n from CRM_Customer_grow");
            if (strWhere.Trim() != "")
            {
                strSql_grid.Append(" WHERE " + strWhere);
                strSql_total.Append(" WHERE " + strWhere);
            }
            strSql_grid.Append("  ) as w1  ");
            strSql_grid.Append("WHERE n BETWEEN " + (PageSize * (PageIndex - 1) + 1) + " AND " + PageSize * PageIndex);
            strSql_grid.Append(" ORDER BY " + filedOrder);
            Total = DbHelperSQL.Query(strSql_total.ToString()).Tables[0].Rows[0][0].ToString();
            return DbHelperSQL.Query(strSql_grid.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

